package com.itheima.AOPAdvice;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;

public class AOPAdvice {

    @Pointcut("execution(* com.itheima.service.impl.UserServiceImpl.*(..))")
    public void pt(){}
    @Before("pt()")
    public void before(){
        System.out.println("before is running");
    }
    @AfterReturning("pt()")
    public void afterReturning(){
        System.out.println("afterReturning is running");
    }
    @AfterThrowing(value = "pt()",throwing = "e")
    public void afterThrowing(Throwable e){
        System.out.println("afterThrowing is running");
    }
    @After("pt()")
    private void after() {
        System.out.println("after is running ...");
    }
    @Around("pt()")
    public Object around(ProceedingJoinPoint pjp){
        Signature methodName = pjp.getSignature();
        System.out.println("methodName = " + methodName);
        Object value = null;
        try {
            //前置增强
            //before();
            long start = System.currentTimeMillis();
            //执行目标方法
            value = pjp.proceed();
            //后置增强
            //afterReturn()
            long end = System.currentTimeMillis();
            System.out.println(methodName+"耗时"+(end-start));
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }finally {
            //最终增强
            after();
        }
      return  value;
    }


}
